/*
* @(#)MaintDmsArchiveForm.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsArchive;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.web.AbstractActionForm;

/**
  MaintDmsArchiveForm.java

  This class is the for web form purpose.

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   23/10/2003
  @version     $Revision: 1.12 $
  */

public class MaintDmsArchiveForm extends AbstractActionForm {

  public static final String REVISION = "$Revision: 1.12 $";

  private String archiveName = null;
  private String archiveType = DmsArchive.BACKUP_TYPE;
  private String description = null;
  private String rootID = null;
  private String archiveDateFrom = null;
  private String archiveDateTo = null;
  private String archiveDate=null;
  private String archiveHour=null;
  private String archiveMinute=null;
  private String archvieNow=null;

  private String parentID = null;
  /** Holds value of property periodType.   */
  private String periodType="month";

  private String[] userDefinedFieldID=null;

  /** Information for target directory selection */
  private String targetRootID = null;
  private String destinationID = null;
  private String destinationPath = null;
  
  private String[] basicSelectedID = null;
  private String   addArchiveID = null;

  public MaintDmsArchiveForm() {
    super();
  }

  public String getArchiveName() {
    return(this.archiveName);
  }

  public void setArchiveName(String archiveName) {
    this.archiveName = archiveName;
  }

  public String getArchiveType() {
    return(this.archiveType);
  }

  public void setArchiveType(String archiveType) {
    this.archiveType = archiveType;
  }

  public String getDescription() {
    return(this.description);
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getRootID() {
    return(this.rootID);
  }

  public void setRootID(String rootID) {
    this.rootID = rootID;
  }

  public String getArchiveDateFrom() {
    Calendar cal = Calendar.getInstance();
    if(Utility.isEmpty(periodType)){
      return archiveDateFrom;
    }else if("week".equals(periodType)){
      cal.add(Calendar.DATE,1-cal.get(Calendar.DAY_OF_WEEK));
    }else if("month".equals(periodType)){
      cal.set(Calendar.DATE,1);
    }else if("full".equals(periodType)){
      return null;
    }
    cal.set(Calendar.AM_PM, Calendar.AM);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return TextUtility.formatDate(cal.getTime(), SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));
  }

  public void setArchiveDateFrom(String archiveDateFrom) {
    this.archiveDateFrom = archiveDateFrom;
  }

  public String getArchiveDateTo() {
    Calendar cal = Calendar.getInstance();
    if(Utility.isEmpty(periodType)){
      return archiveDateTo;
    }else if("week".equals(periodType)){
      cal.add(Calendar.DATE,7-cal.get(Calendar.DAY_OF_WEEK));
    }else if("month".equals(periodType)){
      cal.add(Calendar.MONTH,1);
      cal.set(Calendar.DATE,0);
    }else if("full".equals(periodType)){
      return null;
    }
    cal.set(Calendar.AM_PM, Calendar.PM);
    cal.set(Calendar.HOUR, 11);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MILLISECOND, 59);
    return TextUtility.formatDate(cal.getTime(), SystemParameterFactory.getSystemParameter(SystemParameterConstant.DB_DATETIME_FORMAT));
  }

  public void setArchiveDateTo(String archiveDateTo) {
    this.archiveDateTo = archiveDateTo;
  }

  public String getParentID() {
    return (this.parentID);
  }

  public void setParentID(String parentID) {
    this.parentID = parentID;
  }

  /** Getter for property periodType.
   * @return Value of property periodType.
   */
  public String getPeriodType() {
    return this.periodType;
  }
  public void setPeriodType(String periodType){
    this.periodType = periodType;
  }

  public String[] getUserDefinedFieldID() {
    return(this.userDefinedFieldID);
  }

  public void setUserDefinedFieldID(String[] userDefinedFieldID) {
    this.userDefinedFieldID = userDefinedFieldID;
  }

  public void setDestinationID(String destinationID) {
    this.destinationID = destinationID;
  }

  public String getDestinationID() {
    return destinationID;
  }

  public void setTargetRootID(String targetRootID) {
    this.targetRootID = targetRootID;
  }

  public String getTargetRootID() {
    return targetRootID;
  }

  public void setDestinationPath(String destinationPath) {
    this.destinationPath = destinationPath;
  }

  public String getDestinationPath() {
    return destinationPath;
  }
  
  public void setArchiveDate(String archiveDate) {
    this.archiveDate = archiveDate;
  }

  public String getArchiveDate() {
    return archiveDate;
  }
  
  public void setArchiveHour(String archiveHour){
    this.archiveHour=archiveHour;
  }
  
  public String getArchiveHour(){
    return this.archiveHour;
  }
  
  public void setArchiveMinute(String archiveMinute){
    this.archiveMinute=archiveMinute;
  }
  
  public String getArchiveMinute(){
    return this.archiveMinute;
  }
  
  public void setArchiveNow(String archiveNow){
    this.archvieNow=archiveNow;
  }
  
  public String getArchiveNow(){
    return this.archvieNow;
  }

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    return super.validate(mapping, request);
  }

  public void reset(ActionMapping mapping, HttpServletRequest request) {
    super.reset(mapping, request);
    this.setID(null);
    this.setArchiveName(null);
    this.setArchiveType(DmsArchive.BACKUP_TYPE);
    this.setDescription(null);
    //this.setRootID(null);
    this.setArchiveDateFrom(null);
    this.setArchiveDateTo(null);
    this.setRecordStatus(null);
    this.setUpdateCount(null);
    this.setCreatorID(null);
    this.setCreateDate(null);
    this.setUpdaterID(null);
    this.setUpdateDate(null);
    this.setArchiveDate(null);
    this.setArchiveHour(null);
    this.setArchiveMinute(null);
  }

  public AbstractBaseObject getFormData() throws ApplicationException {
    DmsArchive tmpDmsArchive = new DmsArchive();
    tmpDmsArchive.setID(TextUtility.parseIntegerObj(this.getID()));
    tmpDmsArchive.setArchiveName(this.getArchiveName());
    tmpDmsArchive.setArchiveType(this.getArchiveType());
    tmpDmsArchive.setDescription(this.getDescription());
    tmpDmsArchive.setRootID(TextUtility.parseIntegerObj(this.getRootID()));
    tmpDmsArchive.setArchiveDateFrom(parseTimestamp(this.getArchiveDateFrom()));
    tmpDmsArchive.setArchiveDateTo(parseTimestamp(this.getArchiveDateTo()));
    tmpDmsArchive.setRecordStatus(this.getRecordStatus());
    tmpDmsArchive.setUpdateCount(TextUtility.parseIntegerObj(this.getUpdateCount()));
    tmpDmsArchive.setCreatorID(TextUtility.parseIntegerObj(this.getCreatorID()));
    tmpDmsArchive.setCreateDate(parseTimestamp(this.getCreateDate()));
    tmpDmsArchive.setUpdaterID(TextUtility.parseIntegerObj(this.getUpdaterID()));
    tmpDmsArchive.setUpdateDate(parseTimestamp(this.getUpdateDate()));
    return tmpDmsArchive;
  }

  public void setFormData(AbstractBaseObject baseObj) throws ApplicationException {
    DmsArchive tmpDmsArchive = (DmsArchive)baseObj;
    this.setID(TextUtility.formatIntegerObj(tmpDmsArchive.getID()));
    this.setArchiveName(tmpDmsArchive.getArchiveName());
    this.setArchiveType(tmpDmsArchive.getArchiveType());
    this.setDescription(tmpDmsArchive.getDescription());
    this.setRootID(TextUtility.formatIntegerObj(tmpDmsArchive.getRootID()));
    this.setArchiveDateFrom(formatTimestamp(tmpDmsArchive.getArchiveDateFrom()));
    this.setArchiveDateTo(formatTimestamp(tmpDmsArchive.getArchiveDateTo()));
    this.setRecordStatus(tmpDmsArchive.getRecordStatus());
    this.setUpdateCount(TextUtility.formatIntegerObj(tmpDmsArchive.getUpdateCount()));
    this.setCreatorID(TextUtility.formatIntegerObj(tmpDmsArchive.getCreatorID()));
    this.setCreateDate(formatTimestamp(tmpDmsArchive.getCreateDate()));
    this.setUpdaterID(TextUtility.formatIntegerObj(tmpDmsArchive.getUpdaterID()));
    this.setUpdateDate(formatTimestamp(tmpDmsArchive.getUpdateDate()));
  }
  
  public Timestamp getDatetime() {
    StringBuffer fullTime = new StringBuffer();
    fullTime.append(this.archiveDate.substring(0,10)).append(" ")
            .append((this.archiveHour == null) ? "0" : this.archiveHour).append(":")
            .append((this.archiveMinute == null) ? "0" : this.archiveMinute).append(":")
            .append(0);
    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(SystemParameterFactory.getSystemParameter(
                SystemParameterConstant.DB_DATETIME_FORMAT),
                new Locale(SystemParameterFactory.getSystemParameter(SystemParameterConstant.LOCALE)));
    Date tempDate = null;

    try {
        tempDate = formatter.parse(fullTime.toString());
    } catch (java.text.ParseException pe) {
        return null;
    }

    return new Timestamp(tempDate.getTime());
}

	public String[] getBasicSelectedID() {
		return basicSelectedID;
	}

	public void setBasicSelectedID(String[] basicSelectedID) {
		this.basicSelectedID = basicSelectedID;
	}
  
  public String getAddArchiveID() {
    return this.addArchiveID;
  }
  
  public void setAddArchiveID(String addArchiveID) {
    this.addArchiveID = addArchiveID;
  }
}